package com.hehe.controller.Photo;

import com.hehe.POJO.Photo;
import com.hehe.Repository.PhotoRepository;
import com.hehe.Repository.UserRepository;
import com.hehe.Service.CollectService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Objects;
import java.util.Optional;

@Slf4j
@Controller
public class PhotoCollect {

//    照片的收藏

    @Value("${config_file_name}")
    private String config_file_name;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserRepository userRepository;


    //  String username             Integer photo_id
    //  username , photo_id
    @Resource
    private CollectService collectService;

    @Resource
    private PhotoRepository photoRepository;


    @Transactional(isolation = Isolation.SERIALIZABLE)
    @PostMapping("/api/collect/toggle")
    public ResponseEntity<?> toggleFavorite(@RequestParam String username, @RequestParam long photo_id) {
        boolean collected = collectService.isCollect(username, photo_id);
        if (!collected) {
            collectService.addCollect(username, photo_id);
            photoRepository.incrementCollectCount(1, photo_id);
        } else {
            collectService.removeCollect(username, photo_id);
            photoRepository.incrementCollectCount(-1, photo_id);
        }
        Optional<Photo> photo = photoRepository.findById(photo_id);
        return ResponseEntity.ok(photo.get().getCollect());
    }


    @PostMapping("/api/collect/isCollect")
    public ResponseEntity<?> isCollect(@RequestParam String username, @RequestParam Integer photo_id) {
        if (Objects.equals(username, "null")) {
            return ResponseEntity.ok(false);
        }
        boolean collected = collectService.isCollect(username, photo_id);
        return ResponseEntity.ok(collected);
    }


//    @PostMapping("/api/collect/add")
//    public ResponseEntity<Void> addFavorite(@RequestParam String username, @RequestParam Integer photo_id) {
//        collectService.addFavorite(username , photo_id);
//        return ResponseEntity.ok().build();
//    }
//
//    @DeleteMapping("/api/collect/remove")
//    public ResponseEntity<Void> removeFavorite(@RequestParam String username, @RequestParam Integer photo_id) {
//        collectService.removeFavorite(username , photo_id);
//        return ResponseEntity.ok().build();
//    }
//


////    @ModelAttribute("loggedInUser")
////    public Tb_user getLoggedInUser(@CookieValue(value = "username", required = false) String username) {
////        if (username != null) {
////            // Simulate fetching user data from a service or database
////            Tb_user tb_user = new Tb_user();
////            tb_user.setUsername(username);
////            return tb_user;
////        }
////        return null;
////    }
//
//
//    @PostMapping("/login")
//    public String login(@RequestParam String username, @RequestParam String password, HttpServletResponse response, Model model) {
//        User tb_user = userRepository.findByUsername(username);
//        if (tb_user != null && passwordEncoder.matches(password, tb_user.getPassword())) { // 使用 BCrypt 验证密码
//            Cookie cookie = new Cookie("username", tb_user.getUsername());
//            cookie.setMaxAge(15 * 24 * 60 * 60); // 15 days
//            response.addCookie(cookie);
//            return "redirect:/login-success";
//        }
//        model.addAttribute("error", "用户名 或 密码 错误");
//        return "login/login.html";
//    }
//
//
//    @GetMapping("/login-success")
//    public String homePage(HttpServletRequest request) {
////        Cookie[] cookies = request.getCookies();
////        if (cookies != null) {
////            for (Cookie cookie : cookies) {
////                if ("username".equals(cookie.getName())) {
////                    System.out.println("用户已登录，继续处理请求");
////                    return "login-success.html";
////                }
////            }
////        }
////        return "login.html";
//        return "login/login-success.html";
//    }
//
//
//    @GetMapping("/logout")
//    public String logout(HttpServletRequest request, HttpServletResponse response) {
//        Cookie[] cookies = request.getCookies();
//        if (cookies != null) {
//            for (Cookie cookie : cookies) {
//                if ("username".equals(cookie.getName())) {
//                    cookie.setValue(null);
//                    cookie.setMaxAge(0);
//                    response.addCookie(cookie);
//                }
//            }
//        }
//        return "redirect:/";
//    }


}



